<?php
/**
 * author      : Yxhsea.
 * email       : Yxhsea@foxmail.com
 * createTime  : 2017/1/15 20:33
 * description :
 */
namespace Admin\Controller;
use Think\Controller;

/**
 * Class BaseController
 * @package Admin\Controller
 */
class BaseController extends Controller{

    public function _initialize(){

        if(!session('admin_id')){
            $this->error('非法访问！正在跳转登录页面！',U('Login/index'));
        }

        $id = $_SESSION['admin_id'];

        $role_id = M('Admin')->where(['id'=>session('admin_id')])->getField('role_id');
        $auth = M('Role')->where(['id'=>$role_id])->getField('auth');

        $list = unserialize($auth);// 获取当前的权限
        $list = implode(',',$list);
        if(!$list){
            return true;
        }

        $getlist = M('auth')->where('id in ('.$list.')')->select();

        foreach($getlist as $k=>$v){

            $path = $v['path'];
//            var_dump($path);die;
            $pattem = '/\w+/';
            $result = [];

            preg_match_all($pattem,$path,$result);// 正则表达式提取模块名称
            $get_sum_list[] = $result[0][0];

        }

        $controller = CONTROLLER_NAME;

//        var_dump('111');die;
//        var_dump($controller);die;
//        var_dump($get_sum_list);die;
        if(!in_array($controller,$get_sum_list)){
//               var_dump('222');die;
            if(strtoupper($controller) == 'INDEX' || strtoupper($controller) == 'LOGIN'){
                return true;
            }

            $this->error('您没有访问权限！',U('Index/index'));
        }

//        var_dump('33333');die;

    }

    /**
     * 更改状态
     * @param $id   @id
     * @param $tableName @表名
     * @param $status @状态值
     */
    public function setStatus($id,$tableName,$status){
        if(IS_AJAX){
            $data = array(
                'status' => $status
            );
            $res = M($tableName);
            $res->where(['id'=>$id]);
            $res->save($data);
            $sql = $res->getLastSql();
            if($res){
                $this->ajaxReturn(['info'=>'操作成功','status'=>'1','sql'=>$sql]);
            }else{
                $this->ajaxReturn(['info'=>'操作失败','status'=>'0','sql'=>$sql]);
            }
        }else{
            $this->ajaxReturn(['info'=>'非法操作']);
        }
    }

    /**
     * 移除操作
     * @param $id
     * @param $tableName
     * @param $remove
     */
    public function remove($id,$tableName,$remove){
        if(IS_AJAX){
            $data = array(
                'remove' => $remove
            );
            $res = M($tableName);
            $res->where(['id'=>$id]);
            $res->save($data);
            $sql = $res->getLastSql();
            if($res){
                $this->ajaxReturn(['info'=>'操作成功','status'=>'1','sql'=>$sql]);
            }else{
                $this->ajaxReturn(['info'=>'操作失败','status'=>'0','sql'=>$sql]);
            }
        }else{
            $this->ajaxReturn(['info'=>'非法操作']);
        }
    }

    /**
     * 删除
     * @param $id
     * @param $tableName
     */
    public function delete($id,$tableName){
        if(IS_AJAX){
            $res = M($tableName);
            $res->where(['id'=>$id])->delete();
            $sql = $res->getLastSql();
            if($res){
                $this->ajaxReturn(['info'=>'操作成功','status'=>'1','sql'=>$sql]);
            }else{
                $this->ajaxReturn(['info'=>'操作失败','status'=>'0','sql'=>$sql]);
            }
        }else{
            $this->ajaxReturn(['info'=>'非法操作']);
        }
    }
}